package org.easyx.ser.sys.serivce.impl;

import java.util.ArrayList;
import java.util.List;

import org.easyx.core.utils.ObjKit;
import org.easyx.ser.sys.entity.SysResource;
import org.easyx.ser.sys.mapper.SysResourceMapper;
import org.easyx.ser.sys.serivce.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 * 资源 服务实现类
 * </p>
 *
 * @author xiaojl
 * @since 2017-09-11
 */
@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements SysResourceService {
	@Autowired
	private SysResourceMapper resourcesMapper;
	
	@Override
	public List<SysResource> selectListByUserId(Long userId, String resourceType) {
		return resourcesMapper.selectListByUserId(userId,resourceType);
	}
	
	@Override
	public void insertWithCascade(SysResource entity, String gnxz) {
		this.insert(entity);
		
		if(ObjKit.isNotEmpty(entity.getId())){
			long id = entity.getId();
			if(ObjKit.isNotEmpty(gnxz)){
				String pUrl = entity.getUrl();
				int index = pUrl.lastIndexOf("/");
				String url = pUrl.substring(0, index);
				List<SysResource> rsList = new ArrayList<SysResource>();
				if(gnxz.contains("dataGrid")){//			<option value="dataGrid">列表</option>
					SysResource r = new SysResource();
					r.setName("列表");
					r.setUrl(url+"/dataGrid");
					r.setSeq(0);
					r.setIcon("fi-list icon-black");
					r.setResourceType(1); //按钮
					r.setStatus(0);//正常
					r.setPid(id);
					
					rsList.add(r);
				}
				if(gnxz.contains("add")){//			<option value="add">添加</option>
					SysResource r = new SysResource();
					r.setName("添加");
					r.setUrl(url+"/add");
					r.setSeq(1);
					r.setIcon("fi-plus icon-black");
					r.setResourceType(1); //按钮
					r.setStatus(0);//正常
					r.setPid(id);
					
					rsList.add(r);
				}
				if(gnxz.contains("edit")){//			<option value="edit">编辑</option>
					SysResource r = new SysResource();
					r.setName("编辑");
					r.setUrl(url+"/edit");
					r.setSeq(2);
					r.setIcon("fi-pencil icon-black");
					r.setResourceType(1); //按钮
					r.setStatus(0);//正常
					r.setPid(id);
					
					rsList.add(r);
				}
				if(gnxz.contains("delete")){//			<option value="delete">删除</option>
					SysResource r = new SysResource();
					r.setName("删除");
					r.setUrl(url+"/delete");
					r.setSeq(3);
					r.setIcon("fi-x icon-black");
					r.setResourceType(1); //按钮
					r.setStatus(0);//正常
					r.setPid(id);
					
					rsList.add(r);
				}
				if(gnxz.contains("detail")){//			<option value="detail">详情</option>
					SysResource r = new SysResource();
					r.setName("查看详情");
					r.setUrl(url+"/detail");
					r.setSeq(4);
					r.setIcon("fi-clipboard-notes icon-black");
					r.setResourceType(1); //按钮
					r.setStatus(0);//正常
					r.setPid(id);
					
					rsList.add(r);
				}
				
				this.insertBatch(rsList);
			}
		}
		
	}

}
